#define  _CRT_SECURE_NO_WARNINGS
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
 // class Solution {
 //     int getMax(TreeNode* root)
 //     {
 //         int ret = root->val;
 //         TreeNode* left = root->left;
 //         if(left) ret = max(ret, getMax(left));
 //         TreeNode* right = root->right;
 //         if(right) ret = max(ret, getMax(right));
 //         return ret;
 //     }
 //     int getMin(TreeNode* root)
 //     {
 //         int ret = root->val;
 //         TreeNode* left = root->left;
 //         if(left) ret = min(ret, getMin(left));
 //         TreeNode* right = root->right;
 //         if(right) ret = min(ret, getMin(right));
 //         return ret;
 //     }
 // public:
 //     bool isValidBST(TreeNode* root) {
 //         if(!root) return true;
 //         TreeNode* left = root->left;
 //         TreeNode* right = root->right;
 //         if(left && root->val <= getMax(left)) return false;
 //         if(right && root->val >= getMin(right)) return false;
 //         return isValidBST(left) && isValidBST(right);
 //     }
 // };
class Solution {
    int prev = INT_MIN;
    int jug = 0;
public:
    bool isValidBST(TreeNode* root) {
        if (root == nullptr) return true;
        if (!isValidBST(root->left)) return false;
        if (jug++ && root->val <= prev) return false;
        prev = root->val;
        return isValidBST(root->right);
    }
};